﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
using ThisIsELearning.Model;

namespace ThisIsELearning.SystemManagement.Controller
{
    /// <summary>
    /// Summary description for SystemManagementService
    /// </summary>
    [WebService(Namespace = "http://thisiselearning.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class SystemManagementService : System.Web.Services.WebService
    {
        [WebMethod]
        public LearningStyle[] getLearningStyles()
        {
            return DataManagerHelper.listAllLearningStyles().ToArray();
        }

        [WebMethod]
        public int createLearningStyle(string learningStyleName, string learningStyleDescription)
        {
            LearningStyle aLearningStyle = new LearningStyle();
            aLearningStyle.learningStyleName = learningStyleName;
            aLearningStyle.learningStyleDescription = learningStyleDescription;

            DataManager manager = new DataManager();
            manager.AddToLearningStyles(aLearningStyle);
            manager.SaveChanges();

            return aLearningStyle.learningStyleId;
        }

        [WebMethod]
        public void deleteLearningStyle(int learningStyleId)
        {
            DataManager manager = new DataManager();

            var learningStyle = (from ls in manager.LearningStyles
                                 where ls.learningStyleId == learningStyleId
                                 select ls).First() as LearningStyle;

            manager.DeleteObject(learningStyle);

            manager.SaveChanges();
        }

        [WebMethod]
        public void updateLearningStyle(int learningStyleId, string learningStyleName, string learningStyleDesc)
        {
            DataManager manager = new DataManager();

            LearningStyle learningStyle = manager.LearningStyles.Single(ls => ls.learningStyleId == learningStyleId);
            learningStyle.learningStyleName = learningStyleName;
            learningStyle.learningStyleDescription = learningStyleDesc;

            manager.SaveChanges();
        }
        
    }
}
